<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">

</style>
<div class="page-header">
	<h1>
		动态栏目管理
		<a href="#modal-subject" class="btn btn-info a-custom" data-toggle="modal">
			<i class="ace-icon fa fa-plus"></i> 新增栏目
		</a>
	</h1>
</div>

<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>栏目名称</th>
				<th>排序值</th>
				<th>创建时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 新增弹窗 -->
<div id="modal-subject" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button> <span id="school-name"></span>
					栏目管理
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="subject-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">栏目名称：</label>
								<div class="col-sm-6">
									<input class="form-control" type="text" name="title" placeholder="请填写栏目名称"
										
									/>
								</div>
							</div>
                            <div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">栏目排序：</label>
								<div class="col-sm-6">
									<input class="form-control" type="number" name="sort" value="<?= isset($subject['sort']) ? $subject['sort'] : 0 ?>" placeholder="请填写栏目排序，根据序号从高往低排"
										
									/>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-subject">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){

		// 保存栏目按钮
		var subjectForm = $("#subject-form");
		$("#save-subject").click(function(){
			subjectForm.submit();
		})
		var submitUrl = "<?=admin_url('moral/save_subject_action')?>";
		form_submit(subjectForm, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})
		// 编辑栏目
		$("body").on("click", ".edit-subject", function(){
			subjectForm.find("[name=id]").val($(this).data("id"))
			subjectForm.find("[name=title]").val($(this).data("title"))
			subjectForm.find("[name=sort]").val($(this).data("sort"))
		})

		// 栏目表格
		$('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('moral/get_subject_list')?>',
			"columns": [
	            {"data": "title"},
	            {"data": "sort"},
	            {"data": "createtime"},
	            {"data": function(ret){
	            	var action = '';
	            	action += '<a class="edit-subject green mr20" href="#modal-subject" data-toggle="modal" data-id="'+ ret.id +'" data-title="'+ ret.title +'" data-sort="'+ ret.sort +'"><i class="fa fa-edit"></i> 修改</a>';
					action += '<a class="text-danger delete-subject" href="javascript:" data-id=' + ret.id +  ' data-title=' + ret.title + '><i class="fa fa-times"></i> 删除</a>';
					return action;
	            }},
			]
		});
		// 删除栏目
		$("body").on('click', '.delete-subject', function() {
			var id = $(this).data('id');
			var title = $(this).data('title');
			var posturl = "<?= admin_url("moral/delete_subject_action") ?>";
			var data = {
				'id': id
			};
			layer.confirm('确定删除：' + title + '？', { title:"提示", offset: '160px' }, function(){
				ajax_post(posturl, data, function() {
					// 刷新页面
					window.location.reload();
				});
			});
		});
	
	})
</script>